package utils;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Jasypt 配置
 */
@Configuration
public class JasyptConfig {

    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        // 加密盐值
        config.setPassword("Ayinchd@*(j^z");
        // 加密算法
        config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM);
        // key迭代次数
        config.setKeyObtentionIterations("1000");
        // 池大小
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        // 随机盐生成器
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        // 加密后输出字符串编码方式
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }

    public static void main(String[] args) {
        StringEncryptor encryptor = new JasyptConfig().stringEncryptor();
        String url = "jdbc:mysql://localhost:3306/epoch10?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        String user = "root";
        String pwd = "123456";
        System.out.println(encryptor.encrypt(url));
        System.out.println(encryptor.encrypt(user));
        System.out.println(encryptor.encrypt(pwd));

        url = "jdbc:mysql://localhost:3306/epoch10?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
        System.out.println(encryptor.encrypt(url));
//        FewOa70geqUWWYPKtO/JAC3D4UjTxYLxp8WbmxHbu+OJTm+AdAx//FOT59okxEYcaQL6m0cCX9AGGMRAd7WV/g9Ec/gRKkXjmeV8EQRPlZy7dwD3G1VHSe9hTmKgWrwl2U2jvTGl8W/CIZT7FWJx2ltupkzj5vyR
//        n5oup1S1XnQnhrmF/TngSXSL0tWMeMDe
//        A73z6z34jYB8qS7Dtr5toCdf0jwUMZUC

    }

}

